/* DannyNiu/NJF, 2021-07-18. Public Domain. */

#include "seed.h"
#include "../0-datum/endian.h"
#include "../0-datum/sbox.c.h"

static inline uint64_t func_F(uint64_t k, uint64_t x);

void SEED_Encrypt(void const *in, void *out, void const *restrict w)
{
    uint64_t const *K = w;
    uint64_t T, L, R;
    int i;

    L = be64toh( ((const uint64_t *)in)[0] );
    R = be64toh( ((const uint64_t *)in)[1] );

    for(i=0; i<15; i++)
    {
        T = R;
        R = L ^ func_F(K[i], R);
        L = T;
    }

    L ^= func_F(K[15], R);

    ((uint64_t *)out)[0] = htobe64(L);
    ((uint64_t *)out)[1] = htobe64(R);
}

void SEED_Decrypt(void const *in, void *out, void const *restrict w)
{
    uint64_t const *K = w;
    uint64_t T, L, R;
    int i;

    L = be64toh( ((const uint64_t *)in)[0] );
    R = be64toh( ((const uint64_t *)in)[1] );

    L ^= func_F(K[15], R);

    for(i=15; i-->0; )
    {
        T = L;
        L = R ^ func_F(K[i], L);
        R = T;
    }

    ((uint64_t *)out)[0] = htobe64(L);
    ((uint64_t *)out)[1] = htobe64(R);
}

static inline uint32_t G(uint32_t x);
static inline uint64_t func_F(uint64_t k, uint64_t x)
{
    uint32_t Ki0 = k >> 32, Ki1 = k;
    uint32_t R0 = x >> 32, R1 = x;

    uint32_t R_0, R_1;

    R_0 = G( G( G((R0 ^ Ki0) ^ (R1 ^ Ki1)) + (R0 ^ Ki0)) +
             G((R0 ^ Ki0) ^ (R1 ^ Ki1)));
    R_0 += G( G((R0 ^ Ki0) ^ (R1 ^ Ki1)) + (R0 ^ Ki0));

    R_1 = G( G( G((R0 ^ Ki0) ^ (R1 ^ Ki1)) + (R0 ^ Ki0)) +
             G((R0 ^ Ki0) ^ (R1 ^ Ki1)));

    return (uint64_t)R_0 << 32 | R_1;
}

#define CALC(u,v,i)                                             \
    ((uint32_t)u * 0x010001 | (uint32_t)v * 0x01000100),        \
        ((uint32_t)i * 0x01010101)

static const alignas(1024) uint32_t table_s[512] = SBOX_WIDENER2(
    CALC,
    0xA9, 0x85, 0xD6, 0xD3, 0x54, 0x1D, 0xAC, 0x25, 0x5D, 0x43, 0x18, 0x1E, 0x51, 0xFC, 0xCA, 0x63,
    0x28, 0x44, 0x20, 0x9D, 0xE0, 0xE2, 0xC8, 0x17, 0xA5, 0x8F, 0x03, 0x7B, 0xBB, 0x13, 0xD2, 0xEE,
    0x70, 0x8C, 0x3F, 0xA8, 0x32, 0xDD, 0xF6, 0x74, 0xEC, 0x95, 0x0B, 0x57, 0x5C, 0x5B, 0xBD, 0x01,
    0x24, 0x1C, 0x73, 0x98, 0x10, 0xCC, 0xF2, 0xD9, 0x2C, 0xE7, 0x72, 0x83, 0x9B, 0xD1, 0x86, 0xC9,
    0x60, 0x50, 0xA3, 0xEB, 0x0D, 0xB6, 0x9E, 0x4F, 0xB7, 0x5A, 0xC6, 0x78, 0xA6, 0x12, 0xAF, 0xD5,
    0x61, 0xC3, 0xB4, 0x41, 0x52, 0x7D, 0x8D, 0x08, 0x1F, 0x99, 0x00, 0x19, 0x04, 0x53, 0xF7, 0xE1,
    0xFD, 0x76, 0x2F, 0x27, 0xB0, 0x8B, 0x0E, 0xAB, 0xA2, 0x6E, 0x93, 0x4D, 0x69, 0x7C, 0x09, 0x0A,
    0xBF, 0xEF, 0xF3, 0xC5, 0x87, 0x14, 0xFE, 0x64, 0xDE, 0x2E, 0x4B, 0x1A, 0x06, 0x21, 0x6B, 0x66,
    0x02, 0xF5, 0x92, 0x8A, 0x0C, 0xB3, 0x7E, 0xD0, 0x7A, 0x47, 0x96, 0xE5, 0x26, 0x80, 0xAD, 0xDF,
    0xA1, 0x30, 0x37, 0xAE, 0x36, 0x15, 0x22, 0x38, 0xF4, 0xA7, 0x45, 0x4C, 0x81, 0xE9, 0x84, 0x97,
    0x35, 0xCB, 0xCE, 0x3C, 0x71, 0x11, 0xC7, 0x89, 0x75, 0xFB, 0xDA, 0xF8, 0x94, 0x59, 0x82, 0xC4,
    0xFF, 0x49, 0x39, 0x67, 0xC0,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, 0xB1, 0xD1, 0x20, 0xD7, 0x02, 0x22, 0x04, 0x68, 0x71,
    0x07, 0xDB, 0x9D, 0x99, 0x61, 0xBE, 0xE6, 0x59, 0xDD, 0x51, 0x90, 0xDC, 0x9A, 0xA3, 0xAB, 0xD0,
    0x81, 0x0F, 0x47, 0x1A, 0xE3, 0xEC, 0x8D, 0xBF, 0x96, 0x7B, 0x5C, 0xA2, 0xA1, 0x63, 0x23, 0x4D,
    0xC8, 0x9E, 0x9C, 0x3A, 0x0C, 0x2E, 0xBA, 0x6E, 0x9F, 0x5A, 0xF2, 0x92, 0xF3, 0x49, 0x78, 0xCC,
    0x15, 0xFB, 0x70, 0x75, 0x7F, 0x35, 0x10, 0x03, 0x64, 0x6D, 0xC6, 0x74, 0xD5, 0xB4, 0xEA, 0x09,
    0x76, 0x19, 0xFE, 0x40, 0x12, 0xE0, 0xBD, 0x05, 0xFA, 0x01, 0xF0, 0x2A, 0x5E, 0xA9, 0x56, 0x43,
    0x85, 0x14, 0x89, 0x9B, 0xB0, 0xE5, 0x48, 0x79, 0x97, 0xFC, 0x1E, 0x82, 0x21, 0x8C, 0x1B, 0x5F,
    0x77, 0x54, 0xB2, 0x1D, 0x25, 0x4F, 0x00, 0x46, 0xED, 0x58, 0x52, 0xEB, 0x7E, 0xDA, 0xC9, 0xFD,
    0x30, 0x95, 0x65, 0x3C, 0xB6, 0xE4, 0xBB, 0x7C, 0x0E, 0x50, 0x39, 0x26, 0x32, 0x84, 0x69, 0x93,
    0x37, 0xE7, 0x24, 0xA4, 0xCB, 0x53, 0x0A, 0x87, 0xD9, 0x4C, 0x83, 0x8F, 0xCE, 0x3B, 0x4A, 0xB7);

static inline uint32_t G(uint32_t x)
{
    uint32_t wX = wsbox(x, table_s);
    // wX = wX >> 16 | wX << 16;
    // wX = (wX & 0xff00ff00) >> 8 | (wX & 0xff00ff) << 8;
    wX = htobe32(le32toh(wX)); // in case them being optimized.

    uint32_t wZ = 0;
    wZ ^= 0x3F3F3F3F & (wX);
    wZ ^= 0xCFCFCFCF & (wX >>  8 | wX << 24);
    wZ ^= 0xF3F3F3F3 & (wX >> 16 | wX << 16);
    wZ ^= 0xFCFCFCFC & (wX >> 24 | wX << 8);
    return wZ;
}

void SEED_KeySched(void const *key, void *restrict w)
{
    uint32_t u, v;
    uint32_t k[4];
    uint64_t *K = w;
    int i;

    static const uint32_t KC[] = {
        0x9E3779B9, 0x3C6EF373,
        0x78DDE6E6, 0xF1BBCDCC,
        0xE3779B99, 0xC6EF3733,
        0x8DDE6E67, 0x1BBCDCCF,
        0x3779B99E, 0x6EF3733C,
        0xDDE6E678, 0xBBCDCCF1,
        0x779B99E3, 0xEF3733C6,
        0xDE6E678D, 0xBCDCCF1B,
    };

    k[0] = be32toh( ((const uint32_t *)key)[0] );
    k[1] = be32toh( ((const uint32_t *)key)[1] );
    k[2] = be32toh( ((const uint32_t *)key)[2] );
    k[3] = be32toh( ((const uint32_t *)key)[3] );

    for(i=0; i<16; i++)
    {
        u = G(k[0] + k[2] - KC[i]);
        v = G(k[1] - k[3] + KC[i]);
        K[i] = (uint64_t)u << 32 | v;

        if( i & 1 )
        {
            u = k[2] << 8 | k[3] >> 24;
            v = k[3] << 8 | k[2] >> 24;
            k[2] = u;
            k[3] = v;
        }
        else
        {
            u = k[0] >> 8 | k[1] << 24;
            v = k[1] >> 8 | k[0] << 24;
            k[0] = u;
            k[1] = v;
        }
    }
}

IntPtr iSEED(int q) { return xSEED(q); }
